package com.rs.service.impl.rs;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rs.dao.PerforscMapper;
import com.rs.model.CommonResult;
import com.rs.model.User;
import com.rs.model.Perforsc;
import com.rs.service.rs.PerforscService;
import com.rs.util.ExcelUtil;
import com.rs.util.TimeTypeConversionUtil;

@Service
public class PerforscServiceImpl implements PerforscService {
	@Autowired
	private PerforscMapper pem;

	/************APP*******************/
	/**
	 * 员工查看自己的任务绩效
	 */
	@Override
	public CommonResult<Perforsc> perforscLookOwnApp(Perforsc perforsc) {
		Perforsc p = pem.perforscLookOwnApp(perforsc);
		return new CommonResult<Perforsc>(200, "查询成功", p);
	}

	/**
	 * 员工查看所有的绩效得分
	 */
	@Override
	public CommonResult<List<Perforsc>> perforscLookAllApp(Perforsc perforsc) {
		List<Perforsc> list = pem.perforscLookAllApp(perforsc.getPe_uid());
		return new CommonResult<List<Perforsc>>(200, "查询成功", list);
	}

	/**
	 * 上级查看自己部门的所有员工绩效
	 */
	@Override
	public CommonResult<List<Perforsc>> perforscSuperiorLookAPP(Perforsc perforsc) {
		if (Optional.ofNullable(perforsc.getPe_month()).isPresent()) {

			//设置月份
			perforsc.setPe_month(TimeTypeConversionUtil.conversionTime(perforsc.getPe_month()));
		}
		List<Perforsc> list = pem.perforscSuperiorLookAPP(perforsc);
		return new CommonResult<List<Perforsc>>(200, "查询成功", list);
	}

	/******pc*****/
	/**
	 * pc下载
	 */
	@Override
	public CommonResult<String> perforscDownloadPC(Perforsc perforsc) {
		List<Perforsc> list = pem.perforscConditionsQueryPc(perforsc);
		if (list.size() == 0) {
			return new CommonResult<String>(200, "暂无数据");
		}
		//将peid设为序号
		for (int i = 0, j = list.size(); i < j; i++) {
			list.get(0).setPe_id(i + 1);
		}
		//得到年月
		String yearMonth = TimeTypeConversionUtil.getYearMonth(list.get(0).getPe_month());
		//临时路径
		String path = "C:/CompayRs/temp/easypoi" + "/融盛公司" + yearMonth + "绩效考核.xls";
		String base64 = ExcelUtil.excelDownload(list, "融盛公司" + yearMonth + "绩效考核", "绩效", path);
		return new CommonResult<String>(200, "下载成功", base64);
	}

	/**
	 * pc条件查询绩效得分
	 */
	@Override
	public CommonResult<Map<String, Object>> perforscConditionsQueryPc(Perforsc perforsc) {
		Page<User> page = PageHelper.startPage(perforsc.getPageNum(), 10);//分页
		List<Perforsc> list = pem.perforscConditionsQueryPc(perforsc);//条件查询
		Map<String, Object> mm = new HashMap<String, Object>();
		mm.put("list", list);
		mm.put("total", page.getTotal());//总条数
		return new CommonResult<Map<String, Object>>(200, "查询成功", mm);
	}

	/**
	 * pc查询绩效详情
	 */
	@Override
	public CommonResult<Perforsc> perforscForDetailsPc(Integer pe_id) {
		Perforsc perforsc = pem.perforscForDetailsPc(pe_id);
		return new CommonResult<Perforsc>(200, "查询成功", perforsc);
	}

}
